<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CommentRequest;
use App\Http\Requests\Api\TopUpRequest;
use App\Http\Requests\Api\WithdrawRequest;
use App\Models\Comment;
use App\Models\LogBalance;
use App\Models\Payment;
use App\Models\User;
use App\Transformers\CommentTransformer;
use App\Transformers\PaymentTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class PaymentController extends Controller
{
    /**
     * 提现列表
     *
     * @return \Dingo\Api\Http\Response
     */
    public function withdrawList()
    {
        $data = Payment::where('user_id', $this->user->id)
            ->where('action', Payment::WITHDRAW)
            ->paginate();

        return $this->response->paginator($data, new PaymentTransformer(['id', 'account', 'payment_method', 'amount', 'status', 'created_at']));
    }

    public function topUpList()
    {
        $data = Payment::where('user_id', $this->user->id)
            ->where('action', Payment::TOP_UP)
            ->paginate();

        return $this->response->paginator($data, new PaymentTransformer(['id', 'account', 'payment_method', 'amount', 'status', 'created_at']));
    }

    /**
     * 申请提现
     *
     * @param WithdrawRequest $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function createWithdraw(WithdrawRequest $request)
    {
        $data = $request->only(['amount', 'account', 'payment_method']);

        $data['action'] = Payment::WITHDRAW;

        $pay_password = $request->get('pay_password');

        //判断是否有余额
        if ($this->user->wallet < $data['amount']) {
            $this->response->error(trans('api.balance_not_enough'), 200);
        }

        //判断是否设置支付密码
        if (!$this->user->wallet->pay_password) {
            return response(['message' => trans('api.no_pay_password_set'), 'code' => 0, 'status' => false]);
        }

        //判断支付密码
        if (!Hash::check($pay_password, $this->user->wallet->pay_password)) {
            return response(['message' => trans('api.pay_password_error'), 'code' => 0, 'status' => false]);
        }

        DB::transaction(function () use ($data) {
            try {
                $this->user->payment()->create($data);

                //扣钱
                $this->user->wallet->balance -= getMoneyFormat($data['amount']);

                //记录
                $this->user->logBalance()->create([
                    'user_balance' => $this->user->wallet->balance,
                    'action' => LogBalance::WITHDRAW,
                    'amount' => getMoneyFormat($data['amount']),
                ]);

                $this->user->push();

            } catch ( \Exception $exception ) {
                $this->response->error(trans('api.save_failed'), '200');
            }
        });

        return response()->json(['message' => trans('api.save_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
    }

    /**
     * 充值
     *
     * @param TopUpRequest $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function createTopUp(TopUpRequest $request)
    {
        $data = $request->only(['account', 'acc_password', 'payment_method']);

        $data['action'] = Payment::TOP_UP;

        DB::transaction(function () use ($data) {
            try {

                $this->user->payment()->create($data);

            } catch ( \Exception $exception ) {
                $this->response->error(trans('api.save_failed'), '200');
            }
        });

        return response()->json(['message' => trans('api.save_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
    }
}
